package com.njworkorder.HTSingleLogin;

import cn.microvideo.qsc.client.entity.BelongInfo;
import cn.microvideo.qsc.client.entity.Role;
import cn.microvideo.qsc.client.entity.User;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.io.Serial;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 综管用户对象 t_qs_user
 * 
 * @author ht
 * @date 2024-08-04
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString
@Schema(title = "综管用户对象", description = "综管用户对象")
@Slf4j
public class QsUser extends BaseEntity
{
    @Serial
    private static final long serialVersionUID = 1L;

    private static final CasConfig CAS_CONFIG =SpringUtils.getBean(CasConfig.class);



    /** 唯一标识 */
    @Schema(title = "唯一标识", description = "唯一标识")
    private String uuid;

    /** 用户标识 */
    @Excel(name = "用户标识")
    @Schema(title = "用户标识", description = "用户标识")
    private String uid;

    /** 用户名称 */
    @Excel(name = "用户名称")
    @Schema(title="用户名称", description = "用户名称")
    private String name;

    /** 密码 */
    @Excel(name = "密码")
    @Schema(title="密码", description = "密码")
    private String password;

    /** 联系方式 */
    @Excel(name = "联系方式")
    @Schema(title="联系方式", description = "联系方式")
    private String mobile;

    /** 公司ID */
    @Excel(name = "公司ID")
    @Schema(title="公司ID", description = "公司ID")
    private String oid;

    /** 部门ID */
    @Excel(name = "部门ID")
    @Schema(title="部门ID", description = "部门ID")
    private String ouid;

    @Schema(title="角色列表", description = "角色列表")
    private List<Role> roleList;

    @Schema(title = "是否显示标题",description = "是否显示标题")
    private Boolean showTitle;

    @Schema(title = "所属公司名称",description = "所属公司名称")
    private String ownCompanyName;


    private Boolean admin; // 或者使用其他适当的类型



    public void genBody(User user)
    {
        log.info("genBody user:{}", user);
        List<BelongInfo> orgList = user.getOrgList();
        BelongInfo belongInfo = orgList.get(0);
        this.setOid(belongInfo.getOid());
        this.setUid(user.getUid());
        this.setOuid(belongInfo.getOuid());
        this.setUuid(user.getUuid());
        this.setName(user.getName());
        this.setMobile(user.getMobile());
        this.setPassword(SecurityUtils.encryptPassword("jsjk2024"));
//        this.setPassword(user.getPassWord());
    }

    public boolean isAdmin() {
        if (CAS_CONFIG.isCasEnable())
        {
            //系统管理员、应用管理员具有所有权限
            List<Role> collect = this.getRoleList().stream().filter(role -> role.getRoleLX() == 0 || role.getRoleLX() == 1).collect(Collectors.toList());
            return StringUtils.isNotEmpty(collect);
        }
        return this.getUid() != null && (this.getUid().equalsIgnoreCase("admin")||this.getUid().equalsIgnoreCase("jsjkcdq"));
    }

   /* public static boolean isAdmin(String uid)
    {
        return uid != null && uid.equalsIgnoreCase("admin");
    }*/

    public static boolean isAdmin(QsUser user)
    {
        if (CAS_CONFIG.isCasEnable())
        {
            //系统管理员、应用管理员具有所有权限
            List<Role> collect = user.getRoleList().stream().filter(role -> role.getRoleLX() == 0 || role.getRoleLX() == 1).collect(Collectors.toList());
            return StringUtils.isNotEmpty(collect);
        }else {
            return user.getUid() != null && (user.getUid().equalsIgnoreCase("admin")||user.getUid().equalsIgnoreCase("jsjkcdq"));
        }
    }

}
